<?php
/**
 * @name httpSock
 * @desc This fsockopen open http 
 * */
class httpSock
{
	public $url;
	public $query;
	public $port=80;
	public $method = 'GET';
	public $error;
	public $erron;
	public $fp;
	public $state;
	public $response;
	public $request;
	public $cookie;
	public $data;
	private $isHEnd = false;
	/* __construct	*/
	public function __construct($url=null,$port=80)
	{
		$this->url = $url;
		$this->port = $port;
		$this->open($url,$port);
	}
/*	open */
	public function open($url,$port=80,$time=30)
	{
		if($this->fp = fsockopen($url,$port,$this->erron,$this->error,$time))
		{
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 必须设置好url,query
	 * */
	public function exec()
	{
		if($this->open($this->url,$this->port))
		{
			if(empty($this->query)) return false;
			$this->putQuer($this->query,$this->method);
			$this->putCommonHeader();
			if(!empty($this->cookie))
			{
				$this->putCookie($this->cookie);
			}
			foreach ($this->request as $name=> $var)
			{
				$this->putHeader($name,$var);
			}
			$this->endHeader();
			if(!empty($this->data))
			{
				$this->postData($this->data);
			}
			$this->getResponse();
		}else{
			return false;
		}
	}
	public function putHeader($name,$var)
	{
		fwrite("{$name}: {$var}\r\n");
	}
	public function endHeader()
	{
		fwrite($this->fp,"\r\n");
	}
	public function postData($data)
	{
		if(is_array($data))
		{
			foreach ($data as $key => $var)
			{
				$str = "{$key}={$var}&";
			}
			$str = substr($str,0,-1);
		}else{
			$str = $data;
		}
		$str = urlencode($str);
		fwrite($this->fp,"{$str}\r\n\r\n");
	}
	public function putCookie($cookie)
	{
		fwrite($this->fp,"Cookie:{$cookie}\r\n");
	}
	public function putQuer($query,$method='GET')
	{
		$this->isHEnd = false;
		fwrite($this->fp,"{$method} $query HTTP/1.1\r\n");
	}
	private function putCommonHeader()
	{
		fwrite($this->fp,"Host:{$this->url}\r\n");
		fwrite($this->fp,"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2\r\n");
		fwrite($this->fp,"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
		fwrite($this->fp,"Accept-Language:zh-cn,zh;q=0.5\r\n");
		fwrite($this->fp,"Accept-Encoding:gzip,deflate\r\n");
		fwrite($this->fp,"Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7\r\n");
	}
	public function sclose()
	{
		fclose($this->fp);
	}
	public function getResponse()
	{
		list($hv,$this->state,$msg) = explode(' ',fgets($this->fp,1024));
		while (!feof($this->fp))
		{
			$content = explode(':',fgets($fp, 1024));
			if($content == '\r\n')
			{
				$this->isHEnd = true;
			}
			switch ($c[0])
			{
				case 'Set-Cookie':
					$this->response['cookiepath'] = $content[1];
				break;
				case 'Transfer-Encoding':
					$this->response['transfer'] = $content[1];
				break;
				case 'Content-Length':
					$this->response['length'] = $content[1];
				break;
				case 'Content-Type: text/javascript; charset=UTF-8':
					$type = explode(';',$content[1]);
					$this->response['type'] = $type[0];
					list($tmp,$this->response['charset']) = explode('=',$type[1]);
				break;
			}
		}
	}
	public function getData($return=true,$file=null)
	{
		if(!$this->fp) return false;
		if($this->isHEnd)
		{
			if(empty($file))
			{
				$fh = fopen($file,'wb');
				while (!feof($this->fp)) {
					fwrite($fh,fread($this->fp,1024));
				}
				return true;
			}
			if($return)
			{
				while (!feof($this->fp)) {
					$data .= fread($this->fp,1024);
				}
				return $data;
			}
			return true;
		}else{
			return false;
		}
	}
	public function curlGetData($file)
	{
		if(!function_exists('curl_init')) return false;
		$url = $ths->url.$this->query;
		$file = fopen(ROOT.$file,'wb');
		$cp = curl_init();
		curl_setopt($cp,CURLOPT_URL,$url);
		curl_setopt($cp, CURLOPT_HEADER, false);
		curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_USERAGENT, "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2");
		curl_setopt($cp, CURLOPT_COOKIE, $this->response['cookie']);
		curl_setopt($cp, CURLOPT_FILE, $file);
		$orders = curl_exec($cp);
		return $orders;
	}
}